package com.sunday.common.leaf.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * HA锁缓存 - 单例双重锁
 *
 * @author zsy
 * @since 2022/10/26
 */
@Slf4j
public class HALockCache {

    public volatile static Map<String, HALock> HA_LOCK_MAP = new ConcurrentHashMap<>();

    //获取唯一可用的对象
    public static HALock getInstance(String address, String group, String classify) {
        String key = group + "-" + classify;
        HALock haLock = HA_LOCK_MAP.get(key);
        if (haLock == null) {
            synchronized (HALock.class) {
                haLock = HA_LOCK_MAP.get(key);
                if (haLock == null) {
                    haLock = HALock.of(address, group, classify);
                    HA_LOCK_MAP.put(key, haLock);
                }
            }
        }
        return haLock;
    }

}
